import Vue from 'vue'
import VueRouter from 'vue-router'
import page404 from './modules/page404' // 除了404页面与首页同级别，其它页面均是main页面的子页面
import main from './modules/main'

Vue.use(VueRouter)

const routes = [
  ...page404,
  ...main
]

// 刷新页面自动回到顶部
const scrollBehavior = (to, from, savadProsition) => {
  return {
    x: 0,
    y: 0
  }
}

const router = new VueRouter({
  routes,
  scrollBehavior
})

router.beforeEach((to, from, next) => {
  const isLogin = !!localStorage.token
  if (to.path === '/login' || to.path === '/register') {
    next()
  } else {
    isLogin ? next() : next('/login')
  }
})

export default router
